
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="Python">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>controller &#8212; deepaugment 0.2.0 documentation</title>
    <link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
   
  <link rel="stylesheet" href="../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for controller</h1><div class="highlight"><pre>
<span></span><span class="c1"># (C) 2019 Baris Ozmen &lt;hbaristr@gmail.com&gt;</span>

<span class="kn">import</span> <span class="nn">skopt</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="n">AUG_TYPES</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s2">&quot;crop&quot;</span><span class="p">,</span>
    <span class="s2">&quot;gaussian-blur&quot;</span><span class="p">,</span>
    <span class="s2">&quot;rotate&quot;</span><span class="p">,</span>
    <span class="s2">&quot;shear&quot;</span><span class="p">,</span>
    <span class="s2">&quot;translate-x&quot;</span><span class="p">,</span>
    <span class="s2">&quot;translate-y&quot;</span><span class="p">,</span>
    <span class="s2">&quot;sharpen&quot;</span><span class="p">,</span>
    <span class="s2">&quot;emboss&quot;</span><span class="p">,</span>
    <span class="s2">&quot;additive-gaussian-noise&quot;</span><span class="p">,</span>
    <span class="s2">&quot;dropout&quot;</span><span class="p">,</span>
    <span class="s2">&quot;coarse-dropout&quot;</span><span class="p">,</span>
    <span class="s2">&quot;gamma-contrast&quot;</span><span class="p">,</span>
    <span class="s2">&quot;brighten&quot;</span><span class="p">,</span>
    <span class="s2">&quot;invert&quot;</span><span class="p">,</span>
    <span class="s2">&quot;fog&quot;</span><span class="p">,</span>
    <span class="s2">&quot;clouds&quot;</span><span class="p">,</span>
    <span class="s2">&quot;add-to-hue-and-saturation&quot;</span><span class="p">,</span>
    <span class="s2">&quot;coarse-salt-pepper&quot;</span><span class="p">,</span>
    <span class="s2">&quot;horizontal-flip&quot;</span><span class="p">,</span>
    <span class="s2">&quot;vertical-flip&quot;</span><span class="p">,</span>
<span class="p">]</span>

<div class="viewcode-block" id="augment_type_chooser"><a class="viewcode-back" href="../controller.html#controller.augment_type_chooser">[docs]</a><span class="k">def</span> <span class="nf">augment_type_chooser</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;A random function to choose among augmentation types</span>

<span class="sd">    Returns:</span>
<span class="sd">        function object: np.random.choice function with AUG_TYPES input</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="n">AUG_TYPES</span><span class="p">)</span></div>


<div class="viewcode-block" id="Controller"><a class="viewcode-back" href="../controller.html#controller.Controller">[docs]</a><span class="k">class</span> <span class="nc">Controller</span><span class="p">():</span>

    <span class="n">opt</span><span class="o">=</span><span class="kc">None</span> <span class="c1"># used only if method is bayesian optimization</span>
    <span class="n">random_search_space</span><span class="o">=</span><span class="kc">None</span> <span class="c1"># used only if method is random search</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">config</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Initiliaze Controller either as a Bayesian Optimizer or as a Random Search Algorithm</span>

<span class="sd">        Args:</span>
<span class="sd">             config (dict)</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">config</span><span class="p">[</span><span class="s2">&quot;method&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;bayes&quot;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">method</span> <span class="o">=</span> <span class="s2">&quot;bayesian_optimization&quot;</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">init_skopt</span><span class="p">(</span><span class="n">config</span><span class="p">[</span><span class="s2">&quot;opt_initial_points&quot;</span><span class="p">])</span>
        <span class="k">elif</span> <span class="n">config</span><span class="p">[</span><span class="s2">&quot;method&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;random&quot;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">method</span> <span class="o">=</span> <span class="s2">&quot;random_search&quot;</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">init_random_search</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span>

<div class="viewcode-block" id="Controller.init_skopt"><a class="viewcode-back" href="../controller.html#controller.Controller.init_skopt">[docs]</a>    <span class="k">def</span> <span class="nf">init_skopt</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">opt_initial_points</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Initialize as scikit-optimize (skopt) Optimizer with a 5-dimensional search space</span>

<span class="sd">        Aligned with skopt ask-tell design pattern (https://geekyisawesome.blogspot.com/2018/07/hyperparameter-tuning-using-scikit.html)</span>

<span class="sd">        Args:</span>
<span class="sd">            opt_initial_points (int): number of random initial points for the optimizer</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">opt</span> <span class="o">=</span> <span class="n">skopt</span><span class="o">.</span><span class="n">Optimizer</span><span class="p">(</span>
            <span class="p">[</span>
                <span class="n">skopt</span><span class="o">.</span><span class="n">space</span><span class="o">.</span><span class="n">Categorical</span><span class="p">(</span><span class="n">AUG_TYPES</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;aug1_type&quot;</span><span class="p">),</span>
                <span class="n">skopt</span><span class="o">.</span><span class="n">space</span><span class="o">.</span><span class="n">Real</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;aug1_magnitude&quot;</span><span class="p">),</span>
                <span class="n">skopt</span><span class="o">.</span><span class="n">space</span><span class="o">.</span><span class="n">Categorical</span><span class="p">(</span><span class="n">AUG_TYPES</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;aug2_type&quot;</span><span class="p">),</span>
                <span class="n">skopt</span><span class="o">.</span><span class="n">space</span><span class="o">.</span><span class="n">Real</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;aug2_magnitude&quot;</span><span class="p">),</span>
                <span class="n">skopt</span><span class="o">.</span><span class="n">space</span><span class="o">.</span><span class="n">Real</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;portion&quot;</span><span class="p">),</span>
            <span class="p">],</span>
            <span class="n">n_initial_points</span><span class="o">=</span><span class="n">opt_initial_points</span><span class="p">,</span>
            <span class="n">base_estimator</span><span class="o">=</span><span class="s2">&quot;RF&quot;</span><span class="p">,</span>  <span class="c1"># Random Forest estimator</span>
            <span class="n">acq_func</span><span class="o">=</span><span class="s2">&quot;EI&quot;</span><span class="p">,</span>  <span class="c1"># Expected Improvement</span>
            <span class="n">acq_optimizer</span><span class="o">=</span><span class="s2">&quot;auto&quot;</span><span class="p">,</span>
            <span class="n">random_state</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
        <span class="p">)</span></div>

<div class="viewcode-block" id="Controller.init_random_search"><a class="viewcode-back" href="../controller.html#controller.Controller.init_random_search">[docs]</a>    <span class="k">def</span> <span class="nf">init_random_search</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Initializes random search as the search space is list of random functions</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">random_search_space</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">augment_type_chooser</span><span class="p">,</span>
            <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">,</span>
            <span class="n">augment_type_chooser</span><span class="p">,</span>
            <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">,</span>
            <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span>
        <span class="p">]</span></div>

<div class="viewcode-block" id="Controller.ask"><a class="viewcode-back" href="../controller.html#controller.Controller.ask">[docs]</a>    <span class="k">def</span> <span class="nf">ask</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Ask controller for the next hyperparameter search.</span>


<span class="sd">        If Bayesian Optimizer, samples next hyperparameters by its internal statistic calculations (Random Forest Estimators, Gaussian Processes, etc.). If Random Search, samples randomly</span>
<span class="sd">        Based on ask-tell design pattern</span>

<span class="sd">        Returns:</span>
<span class="sd">            list: list of hyperparameters</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">method</span><span class="o">==</span><span class="s2">&quot;bayesian_optimization&quot;</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="o">.</span><span class="n">ask</span><span class="p">()</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">method</span><span class="o">==</span><span class="s2">&quot;random_search&quot;</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span><span class="n">func</span><span class="p">()</span> <span class="k">for</span> <span class="n">func</span> <span class="ow">in</span> <span class="n">random_search_space</span><span class="p">]</span></div>

<div class="viewcode-block" id="Controller.tell"><a class="viewcode-back" href="../controller.html#controller.Controller.tell">[docs]</a>    <span class="k">def</span> <span class="nf">tell</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_hyperparams</span><span class="p">,</span> <span class="n">f_val</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Tells the controller result of previous tried hyperparameters</span>

<span class="sd">        If Bayesian Optimizer, records this results and updates its internal statistical model. If Random Search does nothing, since it will not affect future (random) samples.</span>

<span class="sd">        Args:</span>
<span class="sd">            trial_hyperparams (list): list of tried hyperparamters</span>
<span class="sd">            f_val (float): trial cost</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">method</span><span class="o">==</span><span class="s2">&quot;bayesian_optimization&quot;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="o">.</span><span class="n">tell</span><span class="p">(</span><span class="n">trial_hyperparams</span><span class="p">,</span> <span class="n">f_val</span><span class="p">)</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">method</span><span class="o">==</span><span class="s2">&quot;random_search&quot;</span><span class="p">:</span>
            <span class="k">pass</span> <span class="c1"># no need to tell anythin</span></div></div>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../index.html">deepaugment</a></h1>








<h3>Navigation</h3>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../index.html">Documentation overview</a><ul>
  <li><a href="index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2019, Baris Ozmen.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 1.8.3</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.12</a>
      
    </div>

    

    
  </body>
</html>